import Opt from '@/components/dt/info/Opt.js'
import houseOpt from '@/biz/option/house.js'
import ruleOpt from '@/biz/option/rule.js'
import templateMgr from '@/biz/bonus/template.js'
import levelMgr from '@/biz/bonus/level.js'

class ImportOpt extends Opt {
  
  constructor(info, type) {
    super(info)
    this.type = type
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let title = this.type == 'template' ? '奖金模板' : '职级模板'
    let tmpl = {
      title,
      placeholder: '请选择',
      key: 'tmplId',
      select: {
        options: []
      },
      rules: [
        ruleOpt.required(`请选择${title}`)
      ]
    }
    
    this.all = [
      houseOpt.create({
        title: '项目名称',
        to: (info) => {
          return {
            housesId: info.housesId
          }
        },
        change: (v) => {
          if (v.info.housesId) {
            let list = this.type == 'template' ? templateMgr.listAll : levelMgr.listAll
            list({ housesId: v.info.housesId }).then(res => {
              tmpl.select.options = res.map(i => ({
                key: this.type == 'template' ? i.templateId : i.rankPointsId,
                value: this.type == 'template' ? i.templateName : i.rankPointsName
              }))
            })
          } else {
            tmpl.select.options = []
            this.info.tmplId = null
          }
        }
      }),
      tmpl
    ]
  }
}

export default ImportOpt
